<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputText value="Cadastro de Cliente"></h:outputText>
        </ui:define>
        <ui:define name="body">
            <h:form id="formClientes">
                <div align="left" style="margin-bottom: 15px;">
                <h:commandButton styleClass="btn btn-mini btn-primary" action="#{clienteController.prepareCreate}" value="Cadastrar cliente"/>
                </div>
                <h:panelGroup id="grupClientes">
                    <fieldset>
                        <div align="left">
                            <legend>
                                <h:outputText value="#{clienteController.pagination.pageFirstItem + 1}..#{clienteController.pagination.pageLastItem + 1}/#{clienteController.pagination.itemsCount}"/>&nbsp;
                                <h:commandLink action="#{clienteController.previous}" value="Anterior #{clienteController.pagination.pageSize}" disabled="#{!clienteController.pagination.hasPreviousPage}"/>&nbsp;
                                <h:commandLink action="#{clienteController.next}" value="Próximo #{clienteController.pagination.pageSize}" disabled="#{!clienteController.pagination.hasNextPage}"/>&nbsp;
                            </legend>
                        </div>
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <td></td>
                                    <td  align="left"> <h:outputText value="Nome:" style="font-style: oblique;"/>
                                        <p:inputText value="#{clienteController.nomeFiltro}" 
                                                     id="nomeFiltro" size="20" maxlength="8">
                                            <p:ajax event="blur" listener="#{clienteController.recreateModel()}" update=":formClientes"/>
                                        </p:inputText>
                                    </td>
                                    <td colspan="3" align="left">
                                        <h:outputText value="Telefone:" style="font-style: oblique;"/>
                                        <p:autoComplete value="#{clienteController.telFiltro}"
                                                        completeMethod="#{clienteController.complete}"
                                                        forceSelection="false" 
                                                        id="telefone" size="12" maxlength="8">
                                            <p:ajax event="itemSelect" listener="#{clienteController.recreateModel()}" update=":formClientes"/>
                                        </p:autoComplete>
                                        <p:commandButton styleClass="btn btn-mini btn-primary" action="#{clienteController.recreateModel()}" update=":formClientes" icon="ui-icon-search"/>
                                    </td>
                                </tr>
                                <tr>
                                    <th align="left"><h:outputText value="Código" style="font-style: oblique;"/></th>
                                    <th align="left"><h:outputText value="Nome" style="font-style: oblique;"/></th>
                                    <th align="left"><h:outputText value="Cadastro"/></th>
                                    <th align="left"><h:outputText value="Telefone"/></th>
                                    <th align="left"></th>
                                    <th align="left"></th>
                                    <th align="left"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <ui:repeat value="#{clienteController.items}" var="item">
                                    <tr>
                                        <td align="left"><h:outputText value="#{item.id}"/></td>
                                        <td align="left"><h:outputText value="#{item.nome}" /></td>
                                    <td align="left"><h:outputText value="#{item.dataCadastro}">
                                            <f:convertDateTime pattern="dd/MM/yyyy" />
                                        </h:outputText>
                                    </td>
                                    <td align="left"><ui:repeat value="#{item.telefones}" var="tel">
                                            <h:outputText value="(#{tel.ddd})#{tel.telefone}"/><br/>
                                        </ui:repeat>
                                    </td>
                                    <td><h:commandLink action="#{clienteController.prepareView}" value="Ver cliente"/></td>
                                    <td><h:commandLink action="#{clienteController.prepareEdit}" value="Editar"/></td>
                                    <td><h:commandLink action="#{clienteController.fazerPedido(item)}" value="Fazer pedido"/></td>
                                    </tr>
                                </ui:repeat>
                            </tbody>
                        </table>
                        <h:outputText escape="false" value="Sem dados" rendered="#{categoriaController.items.rowCount == 0}"/>
                    </fieldset>
                </h:panelGroup>
                
            </h:form>
        </ui:define>
    </ui:composition>

</html>
